/** 外部调用方法
 * var xxxMc:egret.Shape = GraphicsUtil.drawRect(x坐标，y坐标，宽度，长度，颜色，透明度)；
 */
class GraphicsUtil extends egret.Shape {
    public constructor() {
        super();
    }

    /**
	 *  绘制矩形（注册点在左上角）
	 * @param _x 注册点x坐标
	 * @param _y 注册点y坐标
	 * @param _w  宽度
	 * @param _h  高度
	 * @param _color
	 * @param _alp
	 */
    public static drawRect(_x: number, _y: number, _w: number, _h: number, _color: number = 0, _alp: number = 1): egret.Shape {
        var shape: egret.Shape = new egret.Shape();
        shape.x = _x;
        shape.y = _y;
        var g: egret.Graphics = shape.graphics;
        g.beginFill(_color, _alp);
        g.drawRect(0, 0, _w, _h);
        g.endFill();
        return shape;
    }

	/**
	 *  绘制矩形（注册点在中点）
	 * @param _x 中央x坐标
	 * @param _y 中央y坐标
	 * @param _w  宽度
	 * @param _h  高度
	 * @param _color
	 * @param _alp
	 */
    public static drawRect2(_x: number, _y: number, _w: number, _h: number, _color: number = 0, _alp: number = 1): egret.Shape {
        var shape: egret.Shape = new egret.Shape();
        shape.x = _x;
        shape.y = _y;
        var g: egret.Graphics = shape.graphics;
        g.beginFill(_color, _alp);
        g.drawRect(_w / 2, _h / 2, _w, _h);
        g.endFill();
        return shape;
    }
	/**
	 * 绘制圆形 
	 * @param _x 圆心x
	 * @param _y 圆心y
	 * @param _r 半径
	 * @param _color  颜色
	 * @param _alp  透明度
	 */
    public static drawCircle(_x: number, _y: number, _r: number, _color: number = 0, _alp: number = 1): egret.Shape {
        var shape: egret.Shape = new egret.Shape();
        shape.x = _x;
        shape.y = _y;
        var g: egret.Graphics = shape.graphics;
        g.beginFill(_color, _alp);
        g.drawCircle(0, 0, _r);
        g.endFill();
        return shape;
    }
	/**
	 * 绘制正多边形 
	 * @param _x 外接圆圆心x
	 * @param _y 外接圆圆心y
	 * @param _r 外接圆半径
     * @param _color 颜色
	 * @param _sides 正n边形
	 * @param _alp 
	 */
    public static drawPolygon(_x: number, _y: number, _r: number, _color: number = 0, _sides: number = 3, _alp: number = 1): egret.Shape {
        var shape: egret.Shape = new egret.Shape();
        shape.x = _x;
        shape.y = _y;
        var g: egret.Graphics = shape.graphics;
        g.beginFill(_color, _alp);
        for (var i: number = 0; i < _sides; i++) {
            var angle: number = 2 * Math.PI / _sides * i;
            var pa: number = _r * Math.sin(angle);
            var pb: number = -_r * Math.cos(angle);
            if (i == 0) {
                g.moveTo(pa, pb);
            } else {
                g.lineTo(pa, pb);
            }
        }
        g.endFill();
        return shape;
    }
	/**
	 * 绘制星型 
 	 * @param _x 圆心x
	 * @param _b 圆心y
	 * @param _r 内切圆半径
	 * @param _R 外接圆半径
     * @param _color 颜色
	 * @param _sides 边数
	 */
    public static drawStar(_x: number, _y: number, _r: number, _R: number, _color: number = 0, _sides: number = 5, _alp: number = 1): egret.Shape {
        var shape: egret.Shape = new egret.Shape();
        shape.x = _x;
        shape.y = _y;
        var g: egret.Graphics = shape.graphics;
        g.beginFill(_color, _alp);
        for (var i: number = 0; i < _sides * 2; i++) {
            var angle: number = Math.PI / _sides * i;
            var radius: number;
            (i % 2 == 0) ? radius = _R : radius = _r;
            var pa: number = radius * Math.sin(angle);
            var pb: number = radius * Math.cos(angle);
            if (i == 0) {
                g.moveTo(pa, pb);
            } else {
                g.lineTo(pa, pb);
            }
        }
        g.endFill();
        return shape;
    }
    public static drawRoundRect(_x: number, _y: number, _w: number, _h: number, _color: number): egret.Shape {
        var shape: egret.Shape = new egret.Shape();
        shape.x = _x;
        shape.y = _y;
        var g: egret.Graphics = shape.graphics;
        g.beginFill(_color);
        g.drawRoundRect(0, 0, _w, _h, 15, 15);
        g.endFill();

        return shape;
    }
}